const util = require('../../utils/util.js')
var config = require('../../utils/config.js')
const app = getApp();
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    name: '',
    tel: '',
    time: '12:01',
    date: '2018-12-25',
    detail: '',
    index: null,
    picker: [],
  },
  onLoad: function () {
    var TIME = util.formatTime(new Date())
    var t = TIME.split(" ")

    this.setData({
      date: t[0],
      time: t[1],
    })
  },
  onReady:function(){
    this.net_doctorList()
  },
  nameInput(e) {
    this.setData({
      name: e.detail.value
    })
  },
  telInput(e) {
    this.setData({
      tel: e.detail.value
    })
  },
  TimeChange(e) {
    this.setData({
      time: e.detail.value
    })
  },
  DateChange(e) {
    this.setData({
      date: e.detail.value
    })
  },
  textareaAInput(e) {
    this.setData({
      detail: e.detail.value
    })
  },
  PickerChange(e) {
    this.setData({
      index: e.detail.value
    })
  },
  formSubmit: function (e) {
    var that = this
    if (that.data.name.length == 0) {
      wx.showToast({
        title: '请留下您的姓名',
        icon: 'loading',
        duration: 1000
      })
      setTimeout(function () {
        wx.hideToast()
      }, 2000)
      return
    }
    if (that.data.tel.length == 0) {
      wx.showToast({
        title: '请留下您的电话',
        icon: 'loading',
        duration: 1000
      })
      setTimeout(function () {
        wx.hideToast()
      }, 2000)
      return
    }
    if (that.data.tel.length != 11) {
      wx.showToast({
        title: '请输入11位手机号码!',
        icon: 'loading',
        duration: 1000
      })
      setTimeout(function () {
        wx.hideToast()
      }, 2000)
      return
    }
    if (that.data.detail.length == 0) {
      wx.showToast({
        title: '请留下您的病情说明',
        icon: 'loading',
        duration: 1000
      })
      setTimeout(function () {
        wx.hideToast()
      }, 2000)
      return
    }

    console.log( '-----' + that.data.index);
    
    if ( that.data.index == null) {
      wx.showToast({
        title: '请选择医师',
        icon: 'loading',
        duration: 1000
      })
      setTimeout(function () {
        wx.hideToast()
      }, 2000)
      return
    }

    let t = (new Date(that.data.date + ' ' + that.data.time)).getTime()
    let uid = wx.getStorageSync('uid')
    let doctor_id = that.data.picker[that.data.index].uid
    let data = {
      uid: uid,
      name: that.data.name,
      tel: that.data.tel,
      time: that.data.time,
      day: that.data.date,
      content: that.data.detail,
      subscribe_time: t,
      doctor_id: doctor_id
    }

    wx.request({
      url: config.siteURL + '/api/Subscribe/add',
      data: data,
      method: 'GET',
      success: function (res) {
        // success
        console.log(JSON.stringify(res))

        wx.showToast({
          title: '预约成功',
          icon: 'loading',
          duration: 1000
        })

        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 1000)

      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  net_doctorList: function() {
    let that = this
    
    wx.request({
      url: config.siteURL + '/api/Subscribe/doctorList',
      data: {
      },
      method: 'GET',
      success: function (res) {
        // success
        
        that.setData({
          picker: res.data.data
        })
        that.onLoad()
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  }
})